package ca.liu.springmvctest.controller;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

import org.slf4j.Logger;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import ca.liu.logger.InjectLogger;

@Controller
@RequestMapping("/movie")
@Scope("session")
public class MovieController {
	
	@InjectLogger
	private Logger logger;
	
	@PostConstruct
	public void init() {
		logger.debug("-------- Init {} --------", this.getClass());
	}

	@PreDestroy
	public void destory() {
		logger.debug("======== destory {} ========", this.getClass());
	}
	
	@RequestMapping(value="/rhello", method = RequestMethod.GET)
	public String redirectToHello(Model model) {
		model.addAttribute("message", "Spring 3 MVC Hello World redirect from MovieController.class!");
		return "redirect:/welcome";
	}
	
	@RequestMapping(value="/hello", method = RequestMethod.GET)
	public String goToHello(Model model) {
		model.addAttribute("message", "Spring 3 MVC Hello World forward from MovieController.class!");
		return "/hello.jsp";
	}
	
	@RequestMapping(value="/{name}", method = RequestMethod.GET)
	public String getMovie(@PathVariable String name, ModelMap model) {
		model.addAttribute("movie", name);
		
		return "/movie";
	}
}
